<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>固件管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css">
    <style>
        :root {
            --primary: #3a86ff;
            --secondary: #ff006e;
            --success: #06d6a0;
            --dark: #1a1a2e;
            --light: #f8f9fa;
            --base-font-size: 1.1rem; /* Increased base font size */
        }

        body {
            font-size: var(--base-font-size);
            background: linear-gradient(135deg, #f5f7fa 0%, #e4edf5 100%);
            min-height: 100vh;
            padding-bottom: 80px; /* Increased padding */
        }

        .navbar-brand {
            font-weight: 700;
            letter-spacing: 1px;
            font-size: 1.5rem; /* Increased navbar brand size */
        }

        .brand-card {
            border-radius: 16px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
            border: none;
            margin-bottom: 25px;
            transition: transform 0.3s ease;
        }

        .brand-card:hover {
            transform: translateY(-5px);
        }

        .brand-header {
            background: linear-gradient(135deg, var(--primary) 0%, #2a75ff 100%);
            padding: 20px 25px;
            color: white;
            position: relative;
        }

        .brand-header::after {
            content: "";
            position: absolute;
            bottom: -10px;
            left: 20px;
            border-left: 10px solid transparent;
            border-right: 10px solid transparent;
            border-top: 10px solid var(--primary);
        }

        .brand-title {
            font-size: 2rem; /* Increased brand title size */
            font-weight: 700;
            margin: 0;
        }

        .latest-firmware {
            padding: 25px;
            background-color: white;
            border-bottom: 1px solid #eee;
        }

        .firmware-item {
            padding: 15px 20px;
            border-bottom: 1px solid #f0f0f0;
            transition: background-color 0.2s;
        }

        .firmware-item:hover {
            background-color: #f8fbff;
        }

        .firmware-version {
            font-weight: 700;
            color: var(--primary);
            font-size: 1.5rem; /* Increased font size for better visibility */
        }

        .firmware-date {
            font-size: 1rem; /* Slightly larger firmware date */
            color: #6c757d;
        }

        .btn-view-all {
            width: 100%;
            padding: 12px;
            border: none;
            background-color: #f8f9fa;
            color: var(--primary);
            font-weight: 500;
            border-radius: 0 0 16px 16px;
            transition: all 0.3s;
        }

        .btn-view-all:hover {
            background-color: #e9ecef;
        }

        .btn-view-all i {
            transition: transform 0.3s;
        }

        .btn-view-all.collapsed i {
            transform: rotate(0deg);
        }

        .btn-view-all:not(.collapsed) i {
            transform: rotate(180deg);
        }

        .action-buttons .btn {
            margin-left: 8px;
            border-radius: 8px;
            min-width: 100px;
        }

        .new-badge {
            background: var(--secondary);
            color: white;
            font-size: 0.75rem;
            font-weight: bold;
            padding: 3px 8px;
            border-radius: 20px;
            position: absolute;
            top: 15px;
            right: 15px;
        }

        .uploader-info {
            display: flex;
            align-items: center;
            margin-top: 8px;
        }

        .uploader-icon {
            width: 28px;
            height: 28px;
            border-radius: 50%;
            background-color: var(--primary);
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 8px;
        }

        .section-title {
            position: relative;
            padding-bottom: 15px;
            margin-bottom: 30px;
            text-align: center;
            font-weight: 700;
            color: var(--dark);
        }

        .section-title::after {
            content: "";
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 100px;
            height: 4px;
            background: linear-gradient(to right, var(--primary), var(--success));
            border-radius: 2px;
        }

        .stats-card {
            background: white;
            border-radius: 12px;
            padding: 20px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
            text-align: center;
            margin-bottom: 20px;
            border: none;
        }

        .stats-icon {
            width: 60px;
            height: 60px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 15px;
            font-size: 1.5rem;
        }

        .stats-icon.primary {
            background-color: rgba(58, 134, 255, 0.15);
            color: var(--primary);
        }

        .stats-icon.success {
            background-color: rgba(6, 214, 160, 0.15);
            color: var(--success);
        }

        .stats-icon.secondary {
            background-color: rgba(255, 0, 110, 0.15);
            color:var(--secondary);
        }

        .stats-number {
            font-size: 2.5rem; /* Increased stats number size */
            font-weight: 700;
            margin-bottom: 50px;
        }

        .stats-label {
            color: #6c757d;
            font-size: 1.1rem; /* Increased stats label size */
        }

        .footer {
            background: var(--dark);
            color: rgba(255, 255, 255, 0.7);
            padding: 25px 0;
            margin-top: 50px;
        }

        .footer a {
            color: rgba(255, 255, 255, 0.8);
            text-decoration: none;
            transition: color 0.3s;
        }

        .footer a:hover {
            color: white;
        }

        .modal-content {
            border-radius: 16px;
            overflow: hidden;
        }

        .modal-header {
            background: linear-gradient(135deg, var(--primary) 0%, #2a75ff 100%);
            color: white;
        }

        .btn-close {
            filter: invert(1);
        }

        .upload-icon {
            width: 80px;
            height: 80px;
            border-radius: 50%;
            background-color: rgba(58, 134, 255, 0.1);
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0;
            margin: 0 auto 20px;
            color: var(--primary);
            font-size: 2rem;
        }

        @media (max-width: 768px) {
            .action-buttons .btn {
                margin-left: 5px;
                margin-bottom: 5px;
                min-width: auto;
            }

            .brand-title {
                font-size: 1.5rem;
            }
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark sticky-top shadow-sm">
        <div class="container">
            <a class="navbar-brand d-flex align-items-center" href="#">
                <i class="bi bi-cpu me-2"></i>
                固件管理系统
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="切换导航">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link active" href="#"><i class="bi bi-house-door me-1"></i> 首页</a>
                    </li>
                    <li class="nav-item"><a class="nav-link" href="#"><i class="bi bi-question-circle me-1"></i> 帮助</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#"><i class="bi bi-gear me-1"></i> 设置</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#"><i class="bi bi-box-arrow-right me-1"></i> 退出</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container py-5">
    <!-- 标题 -->
    <h1 class="text-center mb-1 fw-bold">固件版本管理平台</h1>
        <p class="text-center text-muted mb-5">高效管理多品牌固件版本与更新日志</p>

        <!-- 统计信息 -->
        <div class="row mb-5">
            <div class="col-md-4">
                <div class="stats-card">
                    <div class="stats-icon primary">
                        <i class="bi bi-motherboard"></i>
                    </div>
                    <div class="stats-number">{{ brands_count }}</div>
                    <div class="stats-label">管理品牌</div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="stats-card">
                    <div class="stats-icon success">
                        <i class="bi bi-file-earmark-binary"></i>
                    </div>
                    <div class="stats-number">{{ firmwares_count }}</div>
                    <div class="stats-label">固件版本</div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="stats-card">
                    <div class="stats-icon secondary">
                        <i class="bi bi-person-check"></i>
                    </div>
                    <div class="stats-number">{{ uploaders_count }}</div>
                    <div class="stats-label">维护人员</div>
                </div>
            </div>
        </div>

        <h2 class="section-title">品牌固件列表</h2>

    {% for brand in brands %}
        <!-- 品牌卡片 -->
        <div class="brand-card">
            <div class="brand-header">
                <h3 class="brand-title">{{ brand.name }}</h3>
                <span class="new-badge">最新版本: {{ brand.latest_version }}</span>
            </div>

            <!-- 最新固件 -->
            {% if brand.firmwares %}
            <div class="latest-firmware">
                <div class="row align-items-center">
                    <div class="col-md-3">
                        <div class="firmware-version">{{ brand.firmwares[0].version }}</div>
                        <div class="firmware-date">{{ brand.firmwares[0].upload_date }}</div>
                    </div>
                    <div class="col-md-4">
                        <div class="uploader-info">
                            <div class="uploader-icon">
                                <i class="bi bi-person"></i>
                            </div>
                            <div>{{ brand.firmwares[0].uploader }}</div>
                        </div>
                    </div>
                    <div class="col-md-5 action-buttons text-md-end">
                        <a href="/download/{{ brand.firmwares[0].id }}" class="btn btn-primary">
                            <i class="bi bi-download me-1"></i> 下载固件
                        </a>
                        <button class="btn btn-outline-primary view-changelog" data-id="{{ brand.firmwares[0].id }}">
                            <i class="bi bi-file-text me-1"></i> 更新日志
                        </button>
                        <form method="POST" action="/delete/{{ brand.firmwares[0].id }}" style="display: inline;">
                            <button type="submit" class="btn btn-danger" onclick="return confirm('确定要删除这个固件吗？')">
                                <i class="bi bi-trash me-1"></i> 删除
                            </button>
                        </form>
                    </div>
                </div>
            </div>
            {% else %}
            <div class="latest-firmware text-center py-4">
                暂无固件数据
            </div>
            {% endif %}

            <!-- 折叠区域 -->
            {% if brand.firmwares|length > 1 %}
            <button class="btn-view-all" type="button" data-bs-toggle="collapse" data-bs-target="#collapse{{ brand.id }}">
                查看所有版本 <i class="bi bi-chevron-down"></i>
            </button>

            <div class="collapse" id="collapse{{ brand.id }}">
                {% for firmware in brand.firmwares[1:] %}
                <div class="firmware-item">
                    <div class="row align-items-center">
                        <div class="col-md-3">
                            <div class="firmware-version">{{ firmware.version }}</div>
                            <div class="firmware-date">{{ firmware.upload_date }}</div>
                        </div>
                        <div class="col-md-4">
                            <div class="uploader-info">
                                <div class="uploader-icon">
                                    <i class="bi bi-person"></i>
                                </div>
                                <div>{{ firmware.uploader }}</div>
                            </div>
                        </div>
                        <div class="col-md-5 action-buttons text-md-end">
                            <a href="/download/{{ firmware.id }}" class="btn btn-outline-secondary">
                                <i class="bi bi-download me-1"></i> 下载
                            </a>
                            <button class="btn btn-outline-secondary view-changelog" data-id="{{ firmware.id }}">
                                <i class="bi bi-file-text me-1"></i> 日志
                            </button>
                            <form method="POST" action="/delete/{{ firmware.id }}" style="display: inline;">
                                <button type="submit" class="btn btn-danger" onclick="return confirm('确定要删除这个固件吗？')">
                                    <i class="bi bi-trash me-1"></i> 删除
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
                {% endfor %}
            </div>
            {% endif %}
        </div>
        {% endfor %}

        <!-- 添加固件模态框 -->
        <div class="modal fade" id="addFirmwareModal" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">添加新固件</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <form id="firmwareForm" action="/add_firmware" method="POST" enctype="multipart/form-data">
                            <div class="upload-icon">
                                <i class="bi bi-cloud-arrow-up"></i>
                            </div>

                            <div class="mb-3">
                                <label class="form-label">选择品牌</label>
                                <select class="form-select" name="brand" required>
                                    <option value="" selected disabled>请选择品牌</option>
                                    {% for brand in brands %}
                                    <option value="{{ brand.id }}">{{ brand.name }}</option>
                                    {% endfor %}
                                </select>
                            </div>

                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label class="form-label">版本号</label>
                                    <input type="text" class="form-control" name="version" placeholder="例如: V1.0.0" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="form-label">上传者</label>
                                    <input type="text" class="form-control" name="uploader" placeholder="您的姓名" required>
                                </div>
                            </div>

                            <div class="mb-3">
                                <label class="form-label">更新日志</label>
                                <textarea class="form-control" name="changelog" rows="4" placeholder="请输入更新内容..." required></textarea>
                            </div>

                            <div class="mb-4">
                                <label class="form-label">固件文件</label>
                                <input class="form-control" type="file" name="firmware_file" accept=".bin,.hex,.img" required>
                                <div class="form-text">支持的文件类型: .bin, .hex, .img</div>
                            </div>

                            <div class="mb-3">
                                <label class="form-label">当前系统时间</label>
                                <div id="current-time" class="form-control" readonly></div>
                                <input type="hidden" id="current-time-input" name="timestamp">
                            </div>

                            <div class="d-grid">
                                <button type="submit" class="btn btn-primary btn-lg">上传固件</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <!-- 更新日志模态框 -->
        <div class="modal fade" id="changelogModal" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">更新日志</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <div id="changelogContent" style="white-space: pre-wrap;"></div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                    </div>
                </div>
            </div>
        </div>

        <!-- 添加品牌模态框 -->
        <div class="modal fade" id="addBrandModal" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">添加新品牌</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <form id="brandForm" action="/add_brand" method="POST">
                            <div class="mb-3">
                                <label class="form-label">品牌名称</label>
                                <input type="text" class="form-control" name="brand_name" placeholder="请输入品牌名称" required>
                            </div>
                            <div class="d-grid">
                                <button type="submit" class="btn btn-primary">添加品牌</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <!-- 删除固件模态框 -->
        <div class="modal fade" id="deleteFirmwareModal" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">删除固件</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <form id="deleteFirmwareForm" method="POST">
                            <div class="mb-3">
                                <label class="form-label">用户名</label>
                                <input type="text" class="form-control" name="username" placeholder="请输入用户名" required>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">密码</label>
                                <input type="password" class="form-control" name="password" placeholder="请输入密码" required>
                            </div>
                            <div class="d-grid">
                                <button type="submit" class="btn btn-danger">确认删除</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <!-- 添加品牌按钮 -->
        <div class="d-flex justify-content-end mb-3">
            <button class="btn btn-success" data-bs-toggle="modal" data-bs-target="#addBrandModal">添加品牌</button>
        </div>

        <div class="text-center mt-5">
            <button class="btn btn-success btn-lg px-5" data-bs-toggle="modal" data-bs-target="#addFirmwareModal">
                <i class="bi bi-plus-circle me-2"></i>添加新固件
            </button>
        </div>
    </div>

    <!-- 页脚 -->
    <footer class="footer">
        <div class="container text-center">
            <p class="mb-0">固件管理系统 &copy; 2023 - 版权所有</p>
            <p class="mb-0">技术支持: tech@example.com | 版本: 1.0.0</p>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 消息提示
        document.addEventListener('DOMContentLoaded', function() {
            const flashes = document.querySelectorAll('.alert');
            flashes.forEach(alert => {
                setTimeout(() => {
                    alert.style.opacity = '0';
                    setTimeout(() => alert.remove(), 300);
                }, 3000);
            });

            // 更新日志查看
            document.querySelectorAll('.view-changelog').forEach(button => {
                button.addEventListener('click', function() {
                    const firmwareId = this.getAttribute('data-id');
                    fetch(`/view_changelog/${firmwareId}`)
                        .then(response => response.text())
                        .then(data => {
                            document.getElementById('changelogContent').textContent = data;
                            const modal = new bootstrap.Modal(document.getElementById('changelogModal'));
                            modal.show();
                        });
                });
            });

            // 实时更新系统时间
            const timeElement = document.getElementById('current-time');
            const timeInput = document.getElementById('current-time-input');
            function updateTime() {
                const now = new Date();
                const formattedTime = now.toLocaleString('zh-CN', {
                    year: 'numeric',
                    month: '2-digit',
                    day: '2-digit',
                    hour: '2-digit',
                    minute: '2-digit',
                    second: '2-digit',
                    hour12: false
                });
                timeElement.textContent = formattedTime;
                timeInput.value = formattedTime;
            }
            setInterval(updateTime, 1000);
            updateTime();

            // 删除固件模态框
            const deleteFirmwareModal = document.getElementById('deleteFirmwareModal');
            deleteFirmwareModal.addEventListener('show.bs.modal', function(event) {
                const button = event.relatedTarget;
                const firmwareId = button.getAttribute('data-firmware-id');
                const form = document.getElementById('deleteFirmwareForm');
                form.action = `/delete/${firmwareId}`;
            });
        });
    </script>
</body>
</html>